package com.example.testkafkaproto.producer.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

public class TestProducerInterceptor implements ProducerInterceptor<String,String> {
    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> producerRecord) {

        ProducerRecord<String,String> newProducerRecord = new ProducerRecord(producerRecord.topic(),producerRecord.key(),"changed-" + producerRecord.value());
        return newProducerRecord;
    }

    @Override
    public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {

        if(e != null){
            e.printStackTrace();
        }else{
            System.out.println("拦截发送消息完毕,RecordMetadata:topic=" + recordMetadata.topic() + ",partition=" + recordMetadata.partition() + ",offset=" + recordMetadata.offset() + ",timestamp=" + recordMetadata.timestamp());
        }
    }

    @Override
    public void close() {

        System.out.println("拦截发送消息完毕,close()");
    }

    @Override
    public void configure(Map<java.lang.String, ?> map) {

    }
}
